package
{
	import flash.display.MovieClip;

	public class TurretObject
	{
		public var name:String;
		public var pos:Vector2;
		public var view:Vector2;
		public var targ:Vector2;
		public var aim:Vector2;
		public var closeAimSpeed:Number;
		public var midAimSpeed:Number;
		public var farAimSpeed:Number;
		public var aimSpeed:Number;
		public var outerThreshold:Number;
		public var innerThreshold:Number;
		public var midThreshold:Number;
		public var shotRate:Number;
		public var shotTimer:Number;
		public var fireDelayTimer:Number;
		public var prefireDelay:Number;
		public var postfireDelay:Number;
		public var isFiring:Boolean;
		public var mc:MovieClip;
		public var crosshairMC:MovieClip;

		public function TurretObject()
		{
			this.name = "gauss turret";
			this.pos = new Vector2(21, 12);
			Main.getInstance().objects.Register(this);
			this.view = new Vector2(0, 0);
			this.targ = new Vector2(0, 0);
			this.aim = new Vector2(this.pos.x, this.pos.y);
			this.closeAimSpeed = 0.05;
			this.midAimSpeed = 0.035;
			this.farAimSpeed = 0.03;
			this.aimSpeed = this.farAimSpeed;
			this.outerThreshold = Main.getInstance().tiles.xw * 8;
			this.innerThreshold = Main.getInstance().tiles.xw * 2;
			this.midThreshold = (0.25 * this.outerThreshold) + (0.75 * this.innerThreshold);
			this.outerThreshold = this.outerThreshold * this.outerThreshold;
			this.midThreshold = this.midThreshold * this.midThreshold;
			this.innerThreshold = this.innerThreshold * this.innerThreshold;
			this.shotRate = 60;
			this.shotTimer = 0;
			this.fireDelayTimer = 0;
			this.prefireDelay = 10;
			this.postfireDelay = 10;
			this.isFiring = false;
			this.mc = Main.getInstance().gfx.CreateSprite("debugTurretMC", NinjaGraphicsSystem.LAYER_WALLS);
			this.mc.visible = false;
			this.crosshairMC = Main.getInstance().gfx.CreateSprite("debugTurretCrosshairMC", NinjaGraphicsSystem.LAYER_OBJECTS);
			this.crosshairMC.visible = false;
		}

		public function Destruct():void
		{
			Main.getInstance().gfx.DestroyMC(this.mc);
			this.mc = null;
			Main.getInstance().gfx.DestroyMC(this.crosshairMC);
			this.crosshairMC = null;
		}

		public var Think:Function;
		public var Update:Function;

		public function Init(params):void
		{
			var _local2 = params;
			if (_local2.length == 2)
			{
				this.pos.x = (this.aim.x = _local2[0]);
				this.pos.y = (this.aim.y = _local2[1]);
				Main.getInstance().objects.StartThink(this);
				this.Think = this.Think_Waiting;
				this.mc.x = this.pos.x;
				this.mc.y = this.pos.y;
				this.mc.scaleX = (this.mc.scaleY = Main.getInstance().tiles.xw);
				this.mc.visible = true;
				this.mc.gotoAndStop("turret_idle");
				this.crosshairMC.scaleX = (this.crosshairMC.scaleY = Main.getInstance().tiles.xw * 1.5);
				this.crosshairMC.visible = false;
			}
		}

		public function UnInit():void
		{
			Main.getInstance().objects.EndThink(this);
			Main.getInstance().objects.EndUpdate(this);
			Main.getInstance().objects.EndDraw(this);
		}

		public function DumpInitData():String
		{
			return "" + this.pos.x + NinjaGame.OBJPARAM_SEPERATION_CHAR + this.pos.y;
		}

		public function Draw():void
		{
			this.crosshairMC.x = this.aim.x;
			this.crosshairMC.y = this.aim.y;
		}

		public function IdleAfterDeath():void
		{
			this.StopTargetting();
			Main.getInstance().objects.EndThink(this);
			Main.getInstance().objects.EndDraw(this);
		}

		public function StartFiring():void
		{
			this.crosshairMC.gotoAndStop("prefire");
			this.mc.gotoAndPlay("turret_prefire");
			Main.getInstance().objects.EndThink(this);
			Main.getInstance().objects.EndDraw(this);
			this.isFiring = true;
			this.fireDelayTimer = 0;
			this.Update = this.Update_PreFire;
		}

		public function StopFiring():void
		{
			Main.getInstance().objects.StartThink(this);
			this.Think = this.Think_Targetting;
			this.crosshairMC.gotoAndStop("postfire");
			this.mc.gotoAndPlay("turret_idle");
			this.isFiring = false;
			this.fireDelayTimer = 0;
			this.Update = this.Update_PostFire;
		}

		public function StartTargetting():void
		{
			this.crosshairMC.visible = true;
			this.crosshairMC.gotoAndStop("aim_far");
			this.aimSpeed = this.farAimSpeed;
			this.aim.x = this.pos.x;
			this.aim.y = this.pos.y;
			this.KeepTargetting();
		}

		public function StopTargetting():void
		{
			this.crosshairMC.visible = false;
			Main.getInstance().objects.EndUpdate(this);
			this.Think = this.Think_Waiting;
			Main.getInstance().objects.EndDraw(this);
		}

		public function KeepTargetting():void
		{
			this.shotTimer = this.shotRate;
			this.Update = this.Update_Targetting;
			this.Think = this.Think_Targetting;
			Main.getInstance().objects.StartUpdate(this);
			Main.getInstance().objects.StartDraw(this);
		}

		public function Fire():void
		{
			this.mc.gotoAndPlay("turret_firing");
			if (Main.getInstance().QueryRayObj(this.targ, this.pos, this.aim, PlayerObject.player))
			{
				var _local3 = this.aim.x - this.pos.x;
				var _local2 = this.aim.y - this.pos.y;
				var len = Math.sqrt((_local3 * _local3) + (_local2 * _local2));
				if (len == 0)
				{
					_local3 = 1;
					_local2 = 1;
				}
				else
				{
					_local3 = _local3 / len;
					_local2 = _local2 / len;
				}
				Main.getInstance().game.KillPlayer(NinjaGame.KILLTYPE_HARDBULLET, _local3 * 8, _local2 * 8, this.targ.x, this.targ.y, this);
				this.targ.x = this.targ.x + (_local3 * PlayerObject.player.r);
				this.targ.y = this.targ.y + (_local2 * PlayerObject.player.r);
				var rot = Main.getInstance().NormToRot(_local3, _local2);
			}
			else
			{
				var _local3 = this.aim.x - this.pos.x;
				var _local2 = this.aim.y - this.pos.y;
				var len = Math.sqrt((_local3 * _local3) + (_local2 * _local2));
				if (len == 0)
				{
					_local3 = 1;
					_local2 = 1;
				}
				else
				{
					_local3 = _local3 / len;
					_local2 = _local2 / len;
				}
				var rot = Main.getInstance().NormToRot(-_local3, -_local2);
			}
			Main.getInstance().particles.SpawnTurretBullet(this.pos, this.targ, rot);
			this.StopFiring();
		}

		public function Think_Waiting():void
		{
			if (Main.getInstance().QueryRayObj(this.view, this.pos, PlayerObject.player.pos, PlayerObject.player))
			{
				this.StartTargetting();
			}
		}

		public function Think_Targetting():void
		{
			if (!Main.getInstance().QueryRayObj(this.view, this.pos, PlayerObject.player.pos, PlayerObject.player))
			{
				this.StopTargetting();
			}
		}

		public function Update_Targetting():void
		{
			var px = ((2 * PlayerObject.player.pos.x) - PlayerObject.player.oldpos.x);
			var py = ((2 * PlayerObject.player.pos.y) - PlayerObject.player.oldpos.y);
			var _local2 = this.aim;
			var dx = (_local2.x - px);
			var dy = (_local2.y - py);
			_local2.x = _local2.x - (this.aimSpeed * dx);
			_local2.y = _local2.y - (this.aimSpeed * dy);
			var _local3 = (dx * dx) + (dy * dy);
			if (this.outerThreshold < _local3)
			{
				this.crosshairMC.gotoAndStop("aim_far");
				this.aimSpeed = this.farAimSpeed;
				return;
			}
			else if (_local3 < this.innerThreshold)
			{
				this.shotTimer = this.shotTimer - (2 + (Main.getInstance().game.GetTime() % 4));
			}
			else if (_local3 < this.midThreshold)
			{
				this.crosshairMC.gotoAndStop("aim_near");
				this.aimSpeed = this.closeAimSpeed;
				this.shotTimer = this.shotTimer - (1 + (Main.getInstance().game.GetTime() % 2));
			}
			else
			{
				this.crosshairMC.gotoAndStop("aim_mid");
				this.aimSpeed = this.midAimSpeed;
				this.shotTimer = this.shotTimer - 0.5;
			}
			if (this.shotTimer < 0)
			{
				this.shotTimer = this.shotRate;
				this.StartFiring();
			}
		}

		public function Update_PreFire():void
		{
			this.fireDelayTimer++;
			if (this.prefireDelay <= this.fireDelayTimer)
			{
				if (!Main.getInstance().QueryRayObj(this.view, this.pos, PlayerObject.player.pos, PlayerObject.player))
				{
					this.StopFiring();
				}
				else
				{
					this.Fire();
				}
			}
		}

		public var shotMC:MovieClip;

		public function Update_PostFire():void
		{
			this.fireDelayTimer++;
			this.shotMC.alpha = 100 - (100 * (this.fireDelayTimer / this.postfireDelay));
			if (this.postfireDelay <= this.fireDelayTimer)
			{
				this.shotMC.visible = false;
				if (!Main.getInstance().QueryRayObj(this.view, this.pos, PlayerObject.player.pos, PlayerObject.player))
				{
					this.StopTargetting();
				}
				else
				{
					this.KeepTargetting();
				}
			}
		}
	}
}